package com.tensquare.article.service.impl;

import com.tensquare.article.pojo.Comment;
import com.tensquare.article.repository.CommentRepository;
import com.tensquare.article.service.CommentService;
import com.tensquare.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    //查询所有评论
    @Override
    public List<Comment> findAll() {
        List<Comment> list = commentRepository.findAll();
        //System.out.println(list);
        return list;
    }

    //根据评论id查询评论
    @Override
    public Comment findById(String commentId) {
        Optional<Comment> commentOptional = commentRepository.findById(commentId);
        Comment comment = commentOptional.get(); //返回评论数据
        return comment;
    }

    //新增评论
    @Override
    public void save(Comment comment) {
        //设置分布式id
        comment.set_id(idWorker.nextId()+"");
        //初始化点赞数，发布时间
        comment.setThumbup(0);
        comment.setPublishdate(new Date());
        commentRepository.save(comment); //save方法如果主键不存在，则执行新增
    }

    //修改评论
    @Override
    public void updateById(String commentId, Comment comment) {
        //comment实体设置id
        comment.set_id(commentId);
        commentRepository.save(comment);//save方法如果主键存在，则执行修改
    }

    //删除评论
    @Override
    public void deleteById(String commentId) {
        commentRepository.deleteById(commentId);
    }

    //根据文章id查询评论
    @Override
    public List<Comment> findCommentByArticleId(String articleId) {
        return commentRepository.findCommentsByArticleid(articleId);
    }

    //给评论点赞
    @Override
    public void thumbup(String commentId) {
        /*//先获取评论数据
        Comment comment = commentRepository.findById(commentId).get();
        //给评论点赞数+1(原有评论数+1)
        comment.setThumbup(comment.getThumbup()+1);
        //重写保存评论数据
        commentRepository.save(comment);*/

        /**
         * 优化点赞逻辑
         */
        Query query = new Query();
        //封装修改的条件，集合中的id对应的是commentId
        //根据commentId修改评论点赞数据
        query.addCriteria(Criteria.where("_id").is(commentId));
        Update update = new Update();
        //列值增长。key是需要增长的集合字段，inc是增长的数值
        update.inc("thumbup",1);
        /*
           更新评论数据
         * a)	参数1，要修改的条件:
         * b)	参数2，要怎样修改，修改的值:
         * c)	参数3，MongoDB的集合的名称
         */
        mongoTemplate.updateFirst(query,update,"comment");
    }


}
